package com.cn.xiaonuo.slbfinances.slbwallets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.shopassets.entity.SlShopWallet;
import com.cn.xiaonuo.slbfinances.slbwallets.mapper.SlbShopWalletMapper;
import com.cn.xiaonuo.slbfinances.slbwallets.service.IShopWalletService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Service
public class ShopWalletServiceImpl extends ServiceImpl<SlbShopWalletMapper, SlShopWallet> implements IShopWalletService {

    @Resource
    private SlbShopWalletMapper slbShopWalletMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryShopWalletList(Integer pageNum, Integer pageSize, Integer userId) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            // 设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = slbShopWalletMapper.queryShopWalletList(userId);
            if (maps != null && maps.size() >= 0) {
                PageInfo<Map<String, Object>> pageInfos = new PageInfo<>(maps);
                message.setData(pageInfos);
                message.setSuccess(true);
                message.setCode("200");
                message.setMessage("查询成功");
                return message;
            }
        } catch (NumberFormatException e) {
            message.setSuccess(false);
            message.setMessage("查询失败");
            e.printStackTrace();
        }
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryYEChangeLogList(Integer pageNum, Integer pageSize, Integer userId) {
        BusinessMessage message = new BusinessMessage();
        try {
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            // 设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = slbShopWalletMapper.queryYEChangeLogList(userId);
            if (maps != null && maps.size() >= 0) {
                PageInfo<Map<String, Object>> pageInfos = new PageInfo<>(maps);
                message.setData(pageInfos);
                message.setSuccess(true);
                message.setCode("200");
                message.setMessage("查询成功");
                return message;
            }
        } catch (NumberFormatException e) {
            message.setSuccess(false);
            message.setMessage("查询失败");
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 获取钱包
     * @param userId
     * @return
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public SlShopWallet getByUserId(Integer userId){
        SlShopWallet slShopWallet = slbShopWalletMapper.selectOne(new QueryWrapper<>(new SlShopWallet() {{
            setUserId(userId);
        }}));
        return slShopWallet;
    }
}
